<?php
/**
 * Instead of putting each step on a separate page, each step will post back to this page and be
 * processed in the next step if it passes the previous step.
 */

define("ROOTDIR", dirname(__FILE__)); // Global Constant of root directory

require_once(ROOTDIR."/include/database/class.InstallAndUpgradeQueries.php");
require_once(ROOTDIR."/config/class.Config.php");

$config = new Config();
$installAndUpgrade = new InstallAndUpgradeQueries();

$installAndUpgrade->setDbHost($config->dbHostName);
$installAndUpgrade->setDbUser($config->dbUserName);
$installAndUpgrade->setDbPass($config->dbPassword);
$installAndUpgrade->setDbase($config->dbName);

$complete = false;

if(isset($_POST['submit'])) {
  $installAndUpgrade->createConnection();
  $installAndUpgrade->connectToDatabase();
  
  $upgradeVersion = $_POST['upgradeVersion'];

  // Upgrade from Version 2.2 to Version 3.0
  if($upgradeVersion == "22to30") {
    $installAndUpgrade->upgradeTwoPointTwoToThree();
  }
  
  // Upgrade from Version 2.2 to Version 3.1
  if($upgradeVersion == "22to31") {
    $installAndUpgrade->upgradeTwoPointTwoToThree();
    $installAndUpgrade->upgradeThreeToThreePointOne();
  }
  
  // Upgrade from Version 2.2 to Version 3.2
  if($upgradeVersion == "22to32") {
    $installAndUpgrade->upgradeTwoPointTwoToThree();
    $installAndUpgrade->upgradeThreeToThreePointOne();
    // There are no database upgrades for 3.2
  }
  
  // Upgrade from Version 3.0 to Version 3.1
  if($upgradeVersion == "30to31") {
    $installAndUpgrade->upgradeThreeToThreePointOne();
  }
  
  // Upgrade from Version 3.0 to Version 3.2
  if($upgradeVersion == "30to32") {
    $installAndUpgrade->upgradeThreeToThreePointOne();
    // There are no database upgrades for 3.2
  }
  
  // Upgrade from Version 3.1 to Version 3.2
  if($upgradeVersion == "31to32") {
    // There are no database upgrades for 3.2
    // This is here for consistency reasons
  }

  $complete = true;
}
?>


<?php
/*************************************************
 * Start upgrade form
 *************************************************/
if(!$complete) {
?>
<h1>Global Ban Upgrade</h1>
<p>Both the install.php and upgrade.php files will be deleted after a successful upgrade!</p>
<p>Select the upgrade type you wish to perform!</p>

<form action="upgrade.php" method="post" onsubmit="return confirm('Have you selected the appropriate upgrade option?');">
  Upgrade Type: <select name="upgradeVersion">
                  <option value="22to30" selected>2.2 to 3.0</option>
                  <option value="22to31" selected>2.2 to 3.1</option>
                  <option value="22to32" selected>2.2 to 3.2</option>
                  <option value="30to31" selected>3.0 to 3.1</option>
                  <option value="30to32" selected>3.0 to 3.2</option>
                  <option value="31to32" selected>3.1 to 3.2</option>
                </select>
  <input type="submit" name="submit" value="Upgrade!">
</form>

<?php
}
/*************************************************
 * End upgrade form
 *************************************************/
?>


<?php
/*************************************************
 * Upgrade COMPLETE
 *************************************************/
if($complete) {
?>
<script type="text/javascript">
window.location = "upgradeComplete.php?version=<?=$upgradeVersion?>"
</script>
<?php
}
/*************************************************
 * End Upgrade COMPLETE
 *************************************************/
?>
